import React, {useEffect, useState} from 'react';
import { message,Modal, Form, Input, Select, Button } from 'antd';
import {applySubmit, dictData, dynamicDictSelectData, getNextApproves} from "@/services/prefiling/onlineCollecting";
import { useModel} from 'umi';

// 模拟接口调用函数
const mockApiCall = async (data: any) => {
  console.log('调用接口，提交数据:', data);
  // 这里可以添加实际的接口调用逻辑
  return new Promise((resolve) => setTimeout(() => resolve({ success: true }), 1000));
};

const { Option } = Select;

// 定义组件的 props 类型
type TransferFormModalProps = {
  open: boolean;
  modalParams: any;
  setOpen: (value: boolean) => void;
};

const TransferFormModal: React.FC<TransferFormModalProps> = ({ open,modalParams, setOpen }) => {
  const [form] = Form.useForm();
  const { initialState, setInitialState } = useModel('@@initialState');
  const { menuItems = [], currentUser ,systemConfig,avatarUrl,logoUrl} = initialState || {};
  const [options, setOptions] = useState<{ label: any; value: any; }[]>([])

  const getSelectOptions = async()=>{
    const params = {
      data:{typeCode:modalParams.currentType.typeCode,createUserId:currentUser.userId},
      flowId:50,
      fullId: modalParams.currentType.fullId,
    }
    const res = await getNextApproves(params)

    if (res && res.data && Array.isArray(res.data)) {
      let newOptions = res.data.map(item => ({
        label: item.username,
        value: item.userId
      }));

      // 检查propValue是否在选项中，如果不在则添加

      // if (defaultValue && !newOptions.some(opt => opt.label === defaultValue)) {
      //   newOptions = [...newOptions, {
      //     label: defaultValue,
      //     value: defaultValue
      //   }];
      // }

      setOptions(newOptions);
    }
    // setOptions(res.data.map(item=>({label:item.username,value:item.userId})))
    // console.log(res)
  }

  const handleOk = async () => {
    try {
      const values = await form.validateFields();
      const { selectedRows, currentType, fileState } = modalParams;
      const params = {
        oper:'add',
        fileIds:JSON.stringify(selectedRows.map((item:string) => ({
          typeCode:currentType.typeCode,
          fileId: item,
          arrangeDimension:currentType.arrangeDimension,
          fullId:currentType.fullId,
        }))),
        typeCode:currentType.typeCode||'',
        fullId:currentType.fullId || '',
        currentOptUserName:values.user.label ||'',
        createUserId: currentUser.userId ||'',
        arrangeDimension:currentType.arrangeDimension || 'FILE',
        appler:currentUser.username ||'',
        applyReason:values.applyReason ||'',
        remark:values.remark ||'',
        currentOptUserId:values.user.value ||'',

        // cfmsCollectInfo: JSON.stringify(selectedRows.map((item) => ({
        //   ...currentType,
        //   state: fileState,
        //   fileId: item,
        // })))
      }
      // 调用模拟接口
      const result = await applySubmit(params);
      console.log(result,11)
      if (result.code === 0) {
        setOpen(false);
        form.resetFields();
      }else{
        message.error(result.msg)
      }
    } catch (error) {
      console.error('表单验证失败:', error);
    }
  };
  useEffect(()=>{
    console.log('modalParams==',modalParams)
    console.log('currentUser==',currentUser)
    getSelectOptions()
  },[])

  const handleCancel = () => {
    setOpen(false);
    form.resetFields();
  };

  return (
    <Modal
      title="申请"
      open={open}
      width={800}
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <Form
        form={form}
        style={{ height: 400 }}
        layout="horizontal" // 设置表单布局为水平
        labelCol={{ span: 6 }} // 标签列占 6 列
        wrapperCol={{ span: 18 }} // 表单组件列占 18 列
      >
        <Form.Item
          name="applyReason"
          label="移交内容"
          rules={[{ required: true, message: '请输入移交内容' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="remark"
          label="备注"
        >
          <Input.TextArea />
        </Form.Item>
        <Form.Item
          name="user"
          label="下个节点审批人"
          rules={[{ required: true, message: '请选择一个选项' }]}
        >
          <Select labelInValue options={options}>
            {/*<Option value="option1">选项1</Option>*/}
            {/*<Option value="option2">选项2</Option>*/}
            {/*<Option value="option3">选项3</Option>*/}
          </Select>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default TransferFormModal;